import { h, ref } from 'vue';
import { NTag } from 'naive-ui';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';

export class State {
  public id = 0; // id
  public code = ''; // code
  public name = ''; // name
  public date = ''; // 日期
  public zbcs = 0; // 炸板次数
  public lbNumber = 0; // 连板次数

  public ztStatisticsDays = 0; // 涨停统计-天数范围
  public ztStatisticsNum = 0; // 涨停统计-涨停天数

  public industry = ''; // 所属行业
  public type = 0; // 1 强势股 2 次新 3 炸板
  public newHigh = 0; // 是否新高
  public selectedReason = 0; // 1 60日新高 2 60日新高且近期多次涨停

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

export const rules = {
  code: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入code',
  },
  date: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入日期',
  },
};

export const schemas = ref<FormSchema[]>([
  {
    field: 'id',
    component: 'NInputNumber',
    label: 'id',
    componentProps: {
      placeholder: '请输入id',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

const baseColums = [
  // {
  //   title: 'id',
  //   key: 'id',
  // },
  {
    title: 'code',
    key: 'code',
  },
  {
    title: 'name',
    key: 'name',
  },
  // {
  //   title: '日期',
  //   key: 'date',
  // },
  {
    title: '炸板次数',
    key: 'zbcs',
  },
  // {
  //   title: '连板次数',
  //   key: 'lbNumber',
  // },
  {
    title: '涨停统计天数范围',
    title: '涨停统计',
    key: 'ztStatisticsDays',
    render: (row: any) => {
      return row.ztStatisticsDays + '天' + row.ztStatisticsNum + '板';
    },
  },
  {
    title: '所属行业',
    key: 'industry',
  },
  {
    title: '是否新高',
    key: 'newHigh',
    render: (row: any) => {
      return h(
        NTag,
        {
          type: row.newHigh == 1 ? 'error' : 'info',
        },
        {
          default: () => (row.newHigh == 1 ? '是' : '否'),
        }
      );
    },
  },
];
export const columns = [...baseColums];

export const strongColumns = [
  ...baseColums,
  {
    title: '入选理由',
    key: 'selectedReason',
    render: (row: any) => {
      switch (row.selectedReason) {
        case 1:
          return '60日新高';
        case 2:
          return '近期多次涨停';
        case 3:
          return '60日新高且近期多次涨停';
      }
    },
  },
];

export const subNewColumns = [
  ...baseColums,
  {
    title: '上市日期',
    key: 'startDate',
  },
];
